package com.sptpc.demo1.controller;

import com.sptpc.demo1.controller.requests.AddOrderRequest;
import com.sptpc.demo1.controller.requests.ModifyOrderRequest;
import com.sptpc.demo1.model.Order;
import com.sptpc.demo1.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/")
    private Order addNewOrder(@RequestBody AddOrderRequest request){
        log.info("订单信息：{}",request);
        Order order=new Order();
        BeanUtils.copyProperties(request,order);
        orderService.addOrder(order,request.getItems());
        return order;
    }
    /**
     * 修改
     * put /drink/{"id":"1","price":"9.0"}
     */
    @PutMapping("/")
    public Order modifOrder(ModifyOrderRequest request) {
        log.info("修改的订单状态:{}", request);
        //同通过id查询出订单
        Order order = orderService.getOrderById(request.getId());
        //复制出属性
        BeanUtils.copyProperties(request, order);
        //更新数据
        orderService.updataOrder(order);
        return order;
    }

    /**
     * 删除
     * delete /drink/1
     */
    @DeleteMapping("/{id}")
    public String deleteOrder(@PathVariable Integer id) {
        orderService.deleteByid(id);
        log.info("删除的id为:{}", id);
        return "删除成功";
    }
    @GetMapping("/{customer}")
    public List<Order>getOrdersByCustomer(@PathVariable String customer){
        return orderService.getOrdersByCustomer(customer);

    }
}
